package com.kfm.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet  {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 将请求参数所有内容放到map集合中 Map的key是请求参数的 key Map的value是请求参数对应的value
        Map<String, String[]> parameterMap = req.getParameterMap();
        parameterMap.forEach((k, v) -> System.out.println(k + "==" + Arrays.toString(v)));
        // 获取单个值 如果有多个只拿到一个
        String username = req.getParameter("username");
        System.out.println("username = " + username);
        // 里面的参数和前端提交的input里面的name属性保持一致
        String password = req.getParameter("pass");
        System.out.println("password = " + password);
        String hobby = req.getParameter("hobby");
        System.out.println("hobby = " + hobby);

        // 获取该请求参数对应的多个值
        String[] hobbies = req.getParameterValues("hobby");
        System.out.println("hobby = " + Arrays.toString(hobbies));
        // 获取所有请求参数的 key
        Enumeration<String> parameterNames = req.getParameterNames();
        Iterator<String> iterator = parameterNames.asIterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

    }
}
